<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>分配角色</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="format-detection" content="telephone=no">
	<link rel="stylesheet" href="/plugins/layui/css/layui.css" media="all" />
</head>
<body class="childrenBody">
	<form class="layui-form">
		<div class="layui-form-item">
		 <label class="layui-form-label">分配角色</label>
			 <div class="layui-input-block" id="boxRoleForm">
			 
			 </div>
		</div>
		<div class="layui-form-item">
			<div class="layui-input-block">
				<button class="layui-btn" lay-submit="" lay-filter="updateRoles">立即提交</button>
		    </div>
		</div>
	</form>
	<script type="text/javascript" src="/js/jquery-1.11.2.min.js"></script>
	<script type="text/javascript" src="/plugins/layui/layui.js"></script>
<!-- 	<script type="text/javascript" src="/js/user/newsAdd.js"></script> -->
	
	<script type="text/javascript">
	layui.config({
		 base: '/js/admin/'
	}).extend({
	    ajaxExtention: 'ajaxExtention',//加载自定义扩展，每个业务js都需要加载这个ajax扩展
	    $tool: 'tool'
	}).use(['form','layer','jquery','ajaxExtention','$tool'],function(){
		var form = layui.form,
			layer = parent.layer === undefined ? layui.layer : parent.layer,
			$ = layui.jquery;
		 $tool = layui.$tool;

		 function getCurrentRoles(){
			 var links = [];

					$("#boxRoleForm").find("input:checked").each(function(){
						links.push($(this).val()); 
					})
				return links.join(',');
		 }
	
		//创建一个编辑器
	 	form.on("submit(updateRoles)",function(data){
	 		  var queryArgs = $tool.getQueryParam();//获取查询参数
	 	        var id = queryArgs['id'];
            var roles=$("#boxRoleForm").find("input:checked").length;
            if(roles>1){
                layer.alert("一个用户不能关联多个角色");
                return false;
            }
	 	        var roleid = getCurrentRoles();
	 	      	 var req = {
	 	      			userid:id,
	 	      			roleid: roleid
	 	          };
	 	      
	 	         $.ajax({
		            cache: true,
		            type: "POST",
		            url:'/users/saveUserRoles',
		            contentType:"application/json;charset=UTF-8",
		            data:JSON.stringify(req),
		            async: false,
		            success: function(data) {
		                if(data.code==2000){
		                	 layer.msg("角色更新成功！", {time: 1000}, function () {
	 	                         layer.closeAll("iframe");
	 	                         //刷新父页面
	 	                         parent.location.reload();
	 	                     });
		                }
		            }
		        }) 
	 	        return false;
	 	})
	 	initUserInfo();
	 	 function initUserInfo() {
	        var queryArgs = $tool.getQueryParam();//获取查询参数
	        var id = queryArgs['id'];
	        allotRole(id);
    	}
	 	
	 	  //弹出选择角色的框
	    function allotRole(id){
	        $.ajax({
	            async:false,
	            type : "POST",
	            data:{uid:id},
	            url: '/roles/rolesWithSelected',
	            dataType:'json',
	            success: function(data){
	            	if(data.code==2000){
	            		var roles=data.data;
	            		 $("#boxRoleForm").empty();
	 	                var htm='';
	 	                for(var i=0;i<roles.length;i++){
	 	                    if(roles[i].selected==1){
	 	                    	htm +=' <input type="checkbox" name="roleid"  title="'+roles[i].roledesc+'" value="'+roles[i].id+'" checked> ';
	 	                    }else{
	 	                    	htm +=' <input type="checkbox" name="roleid"  title="'+roles[i].roledesc+'" value="'+roles[i].id+'"> ';
	 	                    }
	 	                }
	 	                $("#boxRoleForm").append(htm);
	 	                form.render();//重新绘制表单，让修改生效
	            	}
	               
	            }
	        });
	 	  }
	 	  
	    //保存角色的选择
	    function saveUserRoles() {
	        $.ajax({
	            cache: true,
	            type: "POST",
	            url:'/users/saveUserRoles',
	            data:$('#boxRoleForm').serialize(),// 你的formid
	            async: false,
	            success: function(data) {
	                if(data=="success"){
	                    layer.msg('保存成功');
	                    $('#selectRole').modal('hide');
	                }else{
	                    layer.msg('保存失败');
	                    $('#selectRole').modal('hide');
	                }
	            }
	        })
	    }
	})
	</script>
</body>
</html>

